Page({
	data: {
		coupons: [],
		page: 0,
		currentTab: 0
	},

	onLoad: function() {
		this.initCoupon();
	},

	initCoupon() {
		const { userId = "" } = wx.getStorageSync("userInfo");
		const that = this;
		if (userId) {
			wx.request({
				url: `https://pms.uxon.net/api/miniapp/couponUser/page`, //仅为示例，并非真实的接口地址
				method: "POST",
				data: {
					page: that.data.page,
					size: 20,
					status: that.data.currentTab,
					userId,
				},
				header: {
					"content-type": "application/x-www-form-urlencoded", //修改此处即可
				},
				success(res) {
					const data = res.data.data.content || [];
					data.forEach((val) => {
						val.miniappCouponDto.startTime = val.miniappCouponDto.startTime.split(
							" "
						)[0];
						val.miniappCouponDto.expireTime = val.miniappCouponDto.expireTime.split(
							" "
						)[0];
					});
					that.setData({
						coupons:that.data.page === 0 ? data : that.data.coupons.concat(data)
					})
				},
			});
		}
	},
	
	toUseCoupon(options) {
		const couponSn = options.currentTarget.dataset.item.couponSn;
		wx.navigateTo({ url: `../couponDetails/couponDetails?couponSn=${couponSn}` });
	},
	
	async onReachBottom() {
		this.data.page += 1;
		this.initCoupon();
	},

	//滑动切换
	swiperTab: function(e) {
		var that = this;
		that.setData({
			currentTba: e.detail.current
		});
	},

	//点击切换
	clickTab: function(e) {
		var that = this;
		if (this.data.currentTab === e.target.dataset.current) {
			return false;
		} else {
			that.setData({
				currentTab: e.target.dataset.current
			})
		}
		this.initCoupon();
	}

})